1 ========================================================================
2 CONSOLE APPLICATION : CppCheckOSVersion Project Overview
3 ========================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 The CppCheckOSVersion demonstrates how to detect the current OS version, and
9 how to make application that checks for the minimum operating system version
10 work with later operating system versions.
12 We call GetVersionEx to detect the current OS version. If compatibility mode
13 is in effect, the GetVersionEx function reports the operating system as it
14 identifies itself, which may not be the operating system that is installed.
15 For example, if compatibility mode is in effect, GetVersionEx reports the
16 operating system that is selected for application compatibility.
18 To compare the current system version to a required version, use the
19 VerifyVersionInfo function instead of using GetVersionEx to perform the
20 comparison yourself. Please note that compatibility mode does not affect the
21 result of VerifyVersionInfo. VerifyVersionInfo always reports the comparison
22 result based on the operating system that is installed.
24 The most common application compatibility issue that users as well as
25 developers face is when an application fails upon checking the operating
26 system version. A lot can go wrong when version checking is misused. A user
27 might experience a silent fail where the application simply fails to load and
28 nothing happens. Or, a user might see a dialog box indicating something to
29 the effect of -you must be running Microsoft Windows XP or later- when in
30 fact, the computer is running Windows 7. Many other consequences to poor
31 version checking can inconvenience users as well.
33 When an application runs on an "incompatible" (due to poor version checking)
34 version of Windows, it will generally display an error message, but it may
35 also exit silently or behave erratically. Often, if we work around the
36 version checking, the application will run well. End-users and IT
37 professionals may apply a fix to let the application think it is running on
38 an older version of Windows.
41 /////////////////////////////////////////////////////////////////////////////
44 1. Detect the current operating system version:
46 OSVERSIONINFOEX osVersionInfo;
47 ZeroMemory(&osVersionInfo, sizeof(OSVERSIONINFOEX));
48 osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
49 GetVersionEx((LPOSVERSIONINFO) &osVersionInfo);
51 2. Check if the current OS is at least Windows XP
53 OSVERSIONINFOEX osVersionInfoToCompare;
54 ZeroMemory(&osVersionInfoToCompare, sizeof(OSVERSIONINFOEX));
55 osVersionInfoToCompare.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
56 osVersionInfoToCompare.dwMajorVersion = 5;
57 osVersionInfoToCompare.dwMinorVersion = 1; // Windows XP
58 osVersionInfoToCompare.wServicePackMajor = 2; // Service Pack 2
59 osVersionInfoToCompare.wServicePackMinor = 0;
61 ULONGLONG comparisonInfo = 0;
62 BYTE conditionMask = VER_GREATER_EQUAL;
63 VER_SET_CONDITION(comparisonInfo, VER_MAJORVERSION, conditionMask);
64 VER_SET_CONDITION(comparisonInfo, VER_MINORVERSION, conditionMask);
65 VER_SET_CONDITION(comparisonInfo, VER_SERVICEPACKMAJOR, conditionMask);
66 VER_SET_CONDITION(comparisonInfo, VER_SERVICEPACKMINOR, conditionMask);
68 if (!VerifyVersionInfo(&osVersionInfoToCompare, VER_MAJORVERSION |
69 VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
72 // Quit the application due to incompatible OS
76 /////////////////////////////////////////////////////////////////////////////
79 MSDN: GetVersionEx Function
80 http://msdn.microsoft.com/en-us/library/ms724451(VS.85).aspx
82 MSDN: Getting the System Version
83 http://msdn.microsoft.com/en-us/library/ms724429(VS.85).aspx
85 MSDN: VerifyVersionInfo Function
86 http://msdn.microsoft.com/en-us/library/ms725492(VS.85).aspx
88 MSDN: Verifying the System Version
89 http://msdn.microsoft.com/en-us/library/ms725491(VS.85).aspx
92 /////////////////////////////////////////////////////////////////////////////